﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Transactions;
using Composite.Data;
using Composite.Data.Transactions;

public partial class Admin_LCAFlow_Controls_AssignNewFlow : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadFlows();
        }
    }

    private void LoadFlows()
    {
        // ddlFlows
        
        using (DataConnection conn = new DataConnection())
        {
            // Flow can be attached to one or more FlowCategories
            //var flows = from fl in conn.Get<LCA.Data.Flow>()
            //            where fl.IsDeleted == false 
            //            orderby fl.Name
            //            select new
            //            {
            //                ID = fl.Id,
            //                Name = fl.Name
            //            };

            // If a flow can be attached with only ONE FlowCategory
            var flows = from fl in conn.Get<LCA.Data.Flow>()
                        join fcf in conn.Get<LCA.Data.FlowCategoryFlow>() on fl.Id equals fcf.Flow into r1
                        from r2 in r1.DefaultIfEmpty()
                        where fl.IsDeleted == false && r2.Flow == null
                        orderby fl.Name
                        select new
                        {
                            ID = fl.Id,
                            Name = fl.Name
                        };


            ddlFlows.DataSource = flows;
            ddlFlows.DataBind();
        }

    }
}